/**
 * <p>Copyright:Copyright(c) 2019</p>
 * <p>Company:Professional</p>
 * <p>Package:org.cesgroup.course.util</p>
 * <p>File:WordConverter.java</p>
 * <p>类更新历史信息</p>
 *
 * @todo chanlong(陈龙) 创建于 Sep 12, 2019 10:16:50 AM
 */
package org.cesgroup.course.converter;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import fr.opensagres.poi.xwpf.converter.core.ImageManager;
import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLConverter;
import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.cesgroup.course.enumeration.FileType;
import org.cesgroup.course.interceptor.Interceptor;
import org.cesgroup.course.interceptor.Invocation;
import org.cesgroup.course.interceptor.annon.Intercepts;
import org.cesgroup.course.interceptor.annon.Signature;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.List;

/**
 * <p>描述: </p>
 * <p>Company: Professional</p>
 *
 * @author chanlong(陈龙)
 * @version 1.0.2019
 * @date Sep 12, 2019 10:16:50 AM
 */
@Order(2)
@Component
@Intercepts({@Signature(type = Converter.class, method = "process", args = {Object.class, List.class})})
public class WordDocxConverter implements Interceptor {

    /**
     * @author chanlong(陈龙)
     * @date Sep 12, 2019 11:22:36 AM
     * @see org.cesgroup.course.interceptor.Interceptor#intercept(org.cesgroup.course.interceptor.Invocation)
     */
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object[] args = invocation.getArgs();
        List arg = (List) args[1];
        if (arg.contains(FileType.docx.name()) && arg.size() == 1) {
            return process((byte[]) args[0]);
        }
        boolean remove = arg.remove(FileType.docx.name());
        Converter converter = (Converter) invocation.getTarget();
        Object process = converter.process(args[0], (List) args[1]);
        /**
         * 重新执行 转换操作，相当于后置处理
         */
        if(remove){
            File file = new File((String) process);
            byte[] bytes = FileUtil.readBytes(file);
            return process(bytes);
        }else{
            return process;
        }
    }

    private Object process(final byte[] data) throws Throwable {
        String staticpath = locations + "/static/";
        String tmpdir = System.getProperty("java.io.tmpdir");
        String destdir = FileUtil.mkdir(staticpath + "/html/").getPath();

        File folder = new File(tmpdir);
        File tempfile = FileUtil.createTempFile(folder);
        File datafile = FileUtil.writeBytes(data, tempfile);
        File destfile = FileUtil.newFile(destdir + "/" + RandomUtil.randomString(6) + ".html");

        XWPFDocument document = new XWPFDocument(new FileInputStream(datafile));
        XHTMLOptions options = XHTMLOptions.create();

        ImageManager imageManager = new ImageManager(new File(destdir), "image/");
        options.setImageManager(imageManager);

        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(destfile), "utf-8");
        XHTMLConverter xhtmlConverter = (XHTMLConverter) XHTMLConverter.getInstance();
        xhtmlConverter.convert(document, outputStreamWriter, options);
        return destfile.getPath();
    }

    @Value("${ces.resources.static-location}")
    private String locations;

    String getLocations() {
        return locations;
    }

    void setLocations(String locations) {
        this.locations = locations;
    }
}
